package com.xs.lomo;

import java.util.ArrayList;

import com.xs.listview.RosterItem;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListViewAdapter extends BaseExpandableListAdapter{

    private Context context = null;
    private ArrayList<String> groups = null;
    private ArrayList<ArrayList<RosterItem>> children = null;
    
    public boolean areAllItemsEnabled()
    {
        return true;
    }
    
    public ListViewAdapter(Context context, 
    						ArrayList<String> groups, 
    						ArrayList<ArrayList<RosterItem>> childs){
    	this.context = context;
    	this.groups = groups;
    	this.children = childs;
    }
    
    public void addItem(RosterItem buddy){
    	if(!groups.contains(buddy.getGroup())){
    		groups.add(buddy.getGroup());
    	}
    	int index = groups.indexOf(buddy.getGroup());
    	if (children.size() < index + 1) {
            children.add(new ArrayList<RosterItem>());
        }

        children.get(index).add(buddy);
    }
    
    public void clearItem(){
    	groups.clear();
    	children.clear();
    }
    
    public Object getChildItemByName(String name){
    	int gCount = 0, g = 0,  
    	    cCount = 0, c = 0;
    	RosterItem buddy = null;
 	
    	if(null == name){
    		return null;
    	}
    	gCount = getGroupCount();
    	for(g=0; g<gCount; g++){
    		cCount = getChildrenCount(g);
    		for(c=0; c<cCount; c++){
    			buddy = (RosterItem)getChild(g, c);
    			if(null == buddy){
    				break;
    			}
    			if(buddy.getName().equals(name)){
    				return buddy;
    			}
    		}
    	}
    	
		return null;
    }
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return children.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		RosterItem buddy = (RosterItem)getChild(groupPosition, childPosition);
		String State;
		if(null == convertView){
			LayoutInflater buddyInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = buddyInflater.inflate(R.layout.childitem, null);
		}
		ImageView childIv = (ImageView)convertView.findViewById(R.id.buddy_iv);
		if(true == buddy.getImageState()){
			childIv.setBackgroundResource(R.drawable.child_image_on);
		}else{
			childIv.setBackgroundResource(R.drawable.child_image_off);
		}
		
		TextView childTv = (TextView)convertView.findViewById(R.id.tvChildName);
		childTv.setText(" "+buddy.getName());
		
		TextView StateTv = (TextView)convertView.findViewById(R.id.tvState);
		State = buddy.getState();
		int iPos = State.indexOf("|");
		if(-1 != iPos){
			State = State.substring(0, iPos);
		}
		StateTv.setText(State);
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return children.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return groups.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return groups.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		String groupNameStr = (String)getGroup(groupPosition);
		String chlidCountStr = ""+getChildrenCount(groupPosition);
		if(null == convertView){
			LayoutInflater groupInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = groupInflater.inflate(R.layout.groupitem, null);			
		}
		
		ImageView groupIv = (ImageView)convertView.findViewById(R.id.ivGroup_id);		
		if(isExpanded){
			groupIv.setBackgroundResource(R.drawable.buddy_item_down);
		}else{
			groupIv.setBackgroundResource(R.drawable.buddy_item_right);
		}
		
		TextView groupNameTv = (TextView)convertView.findViewById(R.id.tvGroupName);
		groupNameTv.setText(groupNameStr);	
		
		TextView ChildCountTv = (TextView)convertView.findViewById(R.id.tvGroupBuddyCount);
		ChildCountTv.setText(chlidCountStr);
		
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}
	public void releaseItem(){
		if(null != groups){
			groups.clear();
			groups = null;
		}
		if(null != children){
			children.clear();
			children = null;
		}
	}
//	-------------------------

}

